/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.cmcc.zysoft.infocollection.mobile.service.MInterfaceLogService;
import com.cmcc.zysoft.infocollection.mobile.service.MResourceService;
import com.cmcc.zysoft.infocollection.model.Areas;
import com.cmcc.zysoft.infocollection.model.Channel;
import com.cmcc.zysoft.infocollection.model.Cities;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.cmcc.zysoft.infocollection.model.Provinces;
import com.cmcc.zysoft.infocollection.model.Resource;
import com.cmcc.zysoft.infocollection.model.ServiceItem;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.service.AreasPcService;
import com.cmcc.zysoft.infocollection.service.ChannelPcService;
import com.cmcc.zysoft.infocollection.service.CitiesPcService;
import com.cmcc.zysoft.infocollection.service.IndustryPcService;
import com.cmcc.zysoft.infocollection.service.MessagePcService;
import com.cmcc.zysoft.infocollection.service.ProvincesPcService;
import com.cmcc.zysoft.infocollection.service.ResourcePcService;
import com.cmcc.zysoft.infocollection.service.ServiceItemPcService;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.cmcc.zysoft.sysmanage.service.SystemUserPCService;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: ResourceController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-28 上午11:25:23
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/resource")
public class ResourceController extends BaseController {

	/**
	 * 属性名称: resourcePcService.
	 * 类       型: ResourcePcService.
	 */
	@javax.annotation.Resource
	private ResourcePcService resourcePcService;
	
	/**
	 * 属性名称: channelPcService.
	 * 类       型: ChannelPcService.
	 */
	@javax.annotation.Resource
	private ChannelPcService channelPcService;
	
	/**
	 * 属性名称: industryPcService.
	 * 类       型: IndustryPcService.
	 */
	@javax.annotation.Resource
	private IndustryPcService industryPcService;
	
	/**
	 * 属性名称: serviceItemPcService.
	 * 类       型: ServiceItemPcService.
	 */
	@javax.annotation.Resource
	private ServiceItemPcService serviceItemPcService;
	
	/**
	 * 属性名称: areasPcService.
	 * 类       型: AreasPcService.
	 */
	@javax.annotation.Resource
	private AreasPcService areasPcService;
	
	/**
	 * 属性名称: citiesPcService.
	 * 类       型: CitiesPcService.
	 */
	@javax.annotation.Resource
	private CitiesPcService citiesPcService;
	
	/**
	 * 属性名称: provincesPcService.
	 * 类       型: ProvincesPcService.
	 */
	@javax.annotation.Resource
	private ProvincesPcService provincesPcService;
	
	/**
	 * 属性名称: systemUserPcService.
	 * 类       型: SystemUserPcService.
	 */
	@javax.annotation.Resource
	private SystemUserPCService systemUserPcService;
	
	/**
	 * 属性名称: messagePcService.
	 * 类       型: MessagePcService.
	 */
	@javax.annotation.Resource
	private MessagePcService messagePcService;
	
	/**
	 * 属性名称: MResourceService.
	 * 类       型: MResourceService.
	 */
	@javax.annotation.Resource
	private MResourceService mResourceService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@javax.annotation.Resource
	private MInterfaceLogService mInterfaceLogService;
	
	/**
	 * 资源列表页面.
	 * @param modelMap modelMap
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main(ModelMap modelMap) {
		List<Map<String, Object>> provinces = this.provincesPcService.provinces();
		List<Map<String, Object>> channels = this.channelPcService.getChannelList();
		modelMap.addAttribute("channels", channels);
		modelMap.addAttribute("provinces", provinces);
		return "/pc/resource/list";
	}
	
	/**
	 * 获取资源分页列表信息.
	 * @param provinceId 省份ID
	 * @param cityId 城市ID
	 * @param channelId 频道ID
	 * @param checkFlag 是否经过审核(0-是,1-否)
	 * @param dataFlag 数据标识(0-新增,1-更新)
	 * @param shopName 商户名称
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return Map<String, Object>
	 */
	@RequestMapping("/list.htm")
	@ResponseBody
	public Map<String, Object> resourcePage(String provinceId, String cityId, String channelId, String checkFlag, String dataFlag, 
			String shopName, String startTime, String endTime, int page, int rows) {
		return this.resourcePcService.resourcePage(provinceId, cityId, channelId, checkFlag, dataFlag, shopName, startTime, endTime, page, rows);
	}
	
	
	/**
	 * 资源详情页面.
	 * @param modelMap modelMap
	 * @param resourceId 资源ID
	 * @return String
	 */
	@RequestMapping("/detail.htm")
	public String detail(ModelMap modelMap, String resourceId) {
		Resource resource = this.resourcePcService.getEntity(resourceId);
		//频道信息
		Channel channel = this.channelPcService.getEntity(resource.getChannelId());
		//行业信息
		Industry industry = this.industryPcService.getEntity(resource.getTradeId());
		//服务项目信息
		ServiceItem serviceItem = this.serviceItemPcService.getEntity(resource.getBelongId());
		//省信息
		if(StringUtils.hasText(resource.getProvinceId())) {
			Provinces provinces = this.provincesPcService.getEntity(Integer.parseInt(resource.getProvinceId()));
			modelMap.addAttribute("provinces", provinces);
		}
		//市信息
		if(StringUtils.hasText(resource.getCityId())) {
			Cities cities = this.citiesPcService.getEntity(Integer.parseInt(resource.getCityId()));
			modelMap.addAttribute("cities", cities);
		}
		//区/县信息
		if(StringUtils.hasText(resource.getDistrictId())) {
			Areas areas = this.areasPcService.getEntity(Integer.parseInt(resource.getDistrictId()));
			modelMap.addAttribute("areas", areas);
		}
		//上报人信息
		if(StringUtils.hasText(resource.getAddMan())) {
			SystemUser systemUser = this.systemUserPcService.getEntity(resource.getAddMan());
			modelMap.addAttribute("systemUser", systemUser);
		}
		//得到氛围数组
		String rs = resource.getFeature();
		String[] features = rs.split(" ");
		//根据资源ID获取资源审核记录.
	    List<Map<String, Object>> messageList = this.messagePcService.getMessageList(resourceId);
		modelMap.addAttribute("features", features);
	    modelMap.addAttribute("messageList", messageList);
		modelMap.addAttribute("resource", resource);
		modelMap.addAttribute("channel", channel);
		modelMap.addAttribute("industry", industry);
		modelMap.addAttribute("serviceItem", serviceItem);
		return "/pc/resource/detail";
	}
	
	/**
	 * 保存资源审核结果.
	 * @param resourceId 资源ID
	 * @param approveResult 审核结果审核结果(0-默认值,1-通过,-1-不通过)
	 * @param approveMsg 审核意见
	 * @return String
	 */
	@RequestMapping("/saveApproveInfo.htm")
	@ResponseBody
	@Transactional
	public String saveApproveInfo(String resourceId, String approveResult, String approveMsg) {
		User user = SecurityContextUtil.getCurrentUser();
		Resource resource = this.resourcePcService.getEntity(resourceId);
		//设置资源的已审核标志(0-已审核,1-未审核)
		resource.setCheckFlag("0");
		resource.setPassFlag(approveResult);
		this.resourcePcService.updateEntity(resource);
		//持久化审核信息至数据库
		this.messagePcService.addMessage(resourceId, resource.getName(), approveResult, approveMsg, user.getUserId(), user.getMobile());
		return "SUCCESS";
	}
	
	/**
	 * 资源审核页面.
	 * @param modelMap modelMap
	 * @param resourceId 资源ID
	 * @return String
	 */
	@RequestMapping("/approve.htm")
	public String approve(ModelMap modelMap, String resourceId) {
		Resource resource = this.resourcePcService.getEntity(resourceId);
		//频道信息
		Channel channel = this.channelPcService.getEntity(resource.getChannelId());
		//行业信息
		Industry industry = this.industryPcService.getEntity(resource.getTradeId());
		//服务项目信息
		ServiceItem serviceItem = this.serviceItemPcService.getEntity(resource.getBelongId());
		//省信息
		if(StringUtils.hasText(resource.getProvinceId())) {
			Provinces provinces = this.provincesPcService.getEntity(Integer.parseInt(resource.getProvinceId()));
			modelMap.addAttribute("provinces", provinces);
		}
		//市信息
		if(StringUtils.hasText(resource.getCityId())) {
			Cities cities = this.citiesPcService.getEntity(Integer.parseInt(resource.getCityId()));
			modelMap.addAttribute("cities", cities);
		}
		//区/县信息
		if(StringUtils.hasText(resource.getDistrictId())) {
			Areas areas = this.areasPcService.getEntity(Integer.parseInt(resource.getDistrictId()));
			modelMap.addAttribute("areas", areas);
		}
		//上报人信息
		if(StringUtils.hasText(resource.getAddMan())) {
			SystemUser systemUser = this.systemUserPcService.getEntity(resource.getAddMan());
			modelMap.addAttribute("systemUser", systemUser);
		}
		//得到氛围数组
		String rs = resource.getFeature();
		String[] features = rs.split(" ");
        //根据资源ID获取资源审核记录.
		List<Map<String, Object>> messageList = this.messagePcService.getMessageList(resourceId);
		modelMap.addAttribute("features", features);
		modelMap.addAttribute("messageList", messageList);
        modelMap.addAttribute("resource", resource);
		modelMap.addAttribute("channel", channel);
		modelMap.addAttribute("industry", industry);
		modelMap.addAttribute("serviceItem", serviceItem);
		return "/pc/resource/approve";
	}
	
	/**
	 * 保存审核结果,并更细商户信息.
	 * @param resourceId 资源ID
	 * @param shopId 商户ID
	 * @param resourceName 商户名称  
	 * @param shortName 别名简称  
	 * @param branchName 分店名称  
	 * @param channelId 所属频道
	 * @param industryId 所属行业
	 * @param serviceItemId 服务项目  
	 * @param telno 联系电话 
	 * @param districtId 区县
	 * @param cityId 市
	 * @param provinceId 省
	 * @param address 详细地址  
	 * @param landmark 标志性建筑 
	 * @param recommendation 推荐点 
	 * @param specialOffer 特色菜 
	 * @param feature 商户氛围 
	 * @param tag 标签 
	 * @param region 商圈 
	 * @param openTime 营业时间 
	 * @param busRoute 公交路线 
	 * @param route 位置描述 
	 * @param chargeType 是否刷卡 
	 * @param cost 消费档次 
	 * @param privateRoom 包间信息 
	 * @param reservation 是否接受预订 
	 * @param deliveryService 是否外送服务 
	 * @param mobilePayment 是否手机支付 
	 * @param parking 停车位  
	 * @param zipcode 邮编  
	 * @param ticketPrice 参考票价  
	 * @param introduction 商户简介  
	 * @param area 商户面积   
	 * @param x 经度
	 * @param y 纬度
	 * @param isOutbound 外呼确认 
	 * @param capacity 客容量 
	 * @param maxCapacity 最大包间容量 
	 * @param minCapacity 最小包间容量 
	 * @param officialSite 官方站点
	 * @param infoMaintain 维护信息 
	 * @param approveResult 审核结果
	 * @param approveMsg 审核意见
	 * @return String
	 */
	@RequestMapping("/update.htm")
	@ResponseBody
	@Transactional
	public String updateResource(String resourceId, String shopId, String resourceName, String shortName, String branchName, 
			String channelId, String industryId, String serviceItemId, String telno, String districtId, String cityId, 
			String provinceId, String address, String landmark, String recommendation, String specialOffer, String feature, 
			String tag, String region, String openTime, String busRoute, String route, String chargeType, String cost, 
			String privateRoom, String reservation, String deliveryService, String mobilePayment, String parking, String zipcode, 
			String ticketPrice, String introduction, String area, String x, String y, String isOutbound, String capacity, 
			String maxCapacity, String minCapacity, String officialSite, String infoMaintain, String approveResult, 
			String approveMsg) {
	 	 String result = "";
	 	 User user = SecurityContextUtil.getCurrentUser();
	 	//商户氛围的处理
		String[] features = feature.split(",");
	    String featureNew = "";
	    for(int i = 0; i < features.length; i++) {
	    	featureNew += features[i] + " ";
	    }
		try {
			result = this.mResourceService.updateResourceLocal(user.getUserId(), shopId, districtId, serviceItemId, resourceName, 
					branchName, shortName, telno, address, region, landmark, zipcode, openTime, busRoute, route, featureNew, 
					recommendation, introduction, tag, specialOffer, parking, cost, chargeType, area, privateRoom, maxCapacity, 
					minCapacity, capacity, reservation, deliveryService, infoMaintain, x, y, mobilePayment);
			/*result = this.mResourceService.addResourceLocal(user.getUserId(), districtId, serviceItemId, resourceName, branchName, 
					shortName, telno, address, region, landmark, zipcode, openTime, busRoute, route, featureNew, recommendation, 
					introduction, tag, specialOffer, parking, cost, chargeType, area, privateRoom, maxCapacity, minCapacity, 
					capacity, reservation, deliveryService, infoMaintain, x, y, mobilePayment);*/
			
			JSONObject object = JSONObject.parseObject(result);
			//向中央信息库中新增商户信息成功,将商户信息持久化至数据库
			if("1".equals(object.get("status").toString())) {
			    //设置更新状态
				Resource resource = this.resourcePcService.getEntity(resourceId);
		        //设置资源的已审核标志(0-已审核,1-未审核)
				resource.setCheckFlag("0");
				resource.setPassFlag(approveResult);
				this.resourcePcService.updateEntity(resource);
				//更新日志
			    this.mInterfaceLogService.addLog(user.getUserId(), user.getCompanyId(), "更新商户信息");
			    //保存审核意见
		        this.messagePcService.addMessage(resourceId, resourceName, approveResult, approveMsg, user.getUserId(), user.getMobile());
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("#更新商户信息失败!\n{}", e.getMessage());
		}
		return result;
	}
	
	/**
	 * 保存资源审核结果.
	 * @param resourceId 资源ID
	 * @param approveResult 审核结果(-1-不通过,1-通过)
	 * @param approveMsg 审核意见
	 * @return String
	 */
	@RequestMapping("/saveApprove.htm")
	@ResponseBody
	@Transactional
	public String saveApprove(String resourceId, String approveResult, String approveMsg) {
		User user = SecurityContextUtil.getCurrentUser();
		try {
			//设置更新状态
			Resource resource = this.resourcePcService.getEntity(resourceId);
	        //设置资源的已审核标志(0-已审核,1-未审核)
			resource.setCheckFlag("0");
			resource.setPassFlag(approveResult);
			this.resourcePcService.updateEntity(resource);
			//保存审核意见
	        this.messagePcService.addMessage(resourceId, resource.getName(), approveResult, approveMsg, user.getUserId(), user.getMobile());
	        return "SUCCESS";
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("#保存资源审核结果失败!\n{}", e.getMessage());
			return "ERROR";
		}
	}
	
	/**
	 * 用户使用情况统计页面.
	 * @return String
	 */
	@RequestMapping("/statistic.htm")
	public String statistic() {
		return "/pc/resource/statistic";
	}
	
	/**
	 * 用户使用情况统计.
	 * @param companyId 企业ID
	 * @param departmentId 部门ID
	 * @param keyword 搜索关键字(用户姓名/手机号)
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return Map<String, Object>
	 */
	@RequestMapping("/statisticInfo.htm")
	@ResponseBody
	public Map<String, Object> statisticInfo(String companyId, String departmentId, String keyword, String startTime, String endTime, int page, int rows) {
		return this.resourcePcService.statisticInfo(companyId, departmentId, keyword, startTime, endTime, page, rows);
	}
	
	/**
	 * 企业统计页面.
	 * @return String
	 */
	@RequestMapping("/companyStat.htm")
	public String companyStat() {
		return "/pc/resource/companyStat";
	}
	
	/**
	 * 企业统计详细信息.
	 * @param companyId 企业ID
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return Map<String, Object>
	 */
	@RequestMapping("/companyStatInfo.htm")
	@ResponseBody
	public Map<String, Object> companyStatInfo(String companyId, String startTime, String endTime, int page, int rows) {
		return this.resourcePcService.companyStatInfo(companyId, startTime, endTime, page, rows);
	}
	
	/**
	 * 部门统计页面.
	 * @param modelMap modelMap
	 * @param companyId 企业ID
	 * @return String
	 */
	@RequestMapping("/deptStat.htm")
	public String deptStat(ModelMap modelMap, String companyId) {
		modelMap.addAttribute("companyId", companyId);
		return "/pc/resource/deptStat";
	}
	
	/**
	 * 部门统计详细信息.
	 * @param companyId 企业ID
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/deptStatInfo.htm")
	@ResponseBody
	public List<Map<String, Object>> deptStatInfo(String companyId, String startTime, String endTime) {
		return this.resourcePcService.deptStatInfo(companyId, startTime, endTime);
	}
	
	/**
	 * 根据资源ID(逻辑)删除商户信息.
	 * @param resourceId 资源ID
	 * @return String
	 */
	@RequestMapping("/remove.htm")
	@ResponseBody
	public String remove(String resourceId) {
		return this.resourcePcService.removeLocal(resourceId);
	}
}